﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses
Imports System.Data
Imports System.Drawing
Imports System.IO

Public Class EditTour1
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session.Item("UserID") Is Nothing Then
                'Response.Redirect("Login.aspx")
            End If
            If Not IsPostBack = True Then
                cmbZones.Enabled = False
                Fill_ComboBox(cmbCategory, "Select Id,Name from ptn_Countries order by Name")
                additem(cmbCategory, "--Select--", 0)
                Fill_ComboBox(cmbMeal, "SELECT [Id],[Name] FROM [ptn_MealType] order by Name")
                If Not Request.QueryString("tid") Is Nothing Then
                    cmbZones.Items.Clear()
                    'Fill_Checkbox(cmbCategory, "Select c.Id, c.Name from Category c inner join TourCategory tc on tc.CountryId=c.Id where tourId=" & Request.QueryString("tid"))
                    fillTour(Request.QueryString("tid"))
                End If
                additem(cmbZones, "--Select--", 0)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillTour(ByVal Id As Integer)
        Dim txt As Array
        txt = RetriveData("SELECT [Id], [Name], [TourCode], [Days], [Nights], [Summary], [Description], [Include], [Exclude], [MetaTitle], [MetaKeywords], [MetaDescription], [IsFeatured], [IsPackage], [IsCruise], [IsSafari], [IsShortBreak], [IsActive], [Vieworder], [CreatedOn], [UpdatedOn],[Thumbnail],[GMap]  FROM [ptn_Tours] where Id=" & Id, 22)

        txtTourCode.Text = txt(2)
        txtName.Text = txt(1)
        txtDays.Text = txt(3)
        txtNights.Text = txt(4)
        txtSummary.Text = txt(5)
        txtDescription.Content = txt(6)
        txtInclude.Content = txt(7)
        txtExclude.Content = txt(8)
        txtMTitle.Text = txt(9)
        txtMKeywords.Text = txt(10)
        txtMDescription.Text = txt(11)
        chkFeatured.Checked = txt(12)
        chkPackage.Checked = txt(13)
        chkCruise.Checked = txt(14)
        chkSafari.Checked = txt(15)
        chkShortBreak.Checked = txt(16)
        chkIsActive.Checked = txt(17)
        txtViewOrder.Text = txt(18)

        ltrImg.Text = "<img src=""../images/img/" & txt(21) & """ align=""absmiddle"" />"
        ltrMap.Text = "<img src=""../images/img/" & txt(22) & """ align=""absmiddle"" />"

        Dim arrTourCat As Array
        arrTourCat = RetriveData("select CountryId, ZoneId from dbo.ptn_CatZoneTours where TourId=" & Id, 1)
        cmbCategory.SelectedValue = arrTourCat(0)

        Fill_ComboBox(cmbZones, "select distinct Id,Name from ptn_Zones z  inner join ptn_CatZoneTours cz on cz.ZoneId=z.Id where cz.CountryId='" & arrTourCat(0) & "' and IsActive=1 order by Name")
        cmbZones.Enabled = True

        cmbZones.SelectedValue = arrTourCat(1)

    End Sub


    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        Try
            Dim intId As Integer
            Dim counter As Integer = 0
            Dim chkActive, strSafari, strFeatured, strPackage, strCruise, strShortBreak As String
            Dim strMeal As String = ""

            If chkFeatured.Checked = True Then
                strFeatured = "1"
            Else
                strFeatured = "0"
            End If
            If chkPackage.Checked = True Then
                strPackage = "1"
            Else
                strPackage = "0"
            End If
            If chkCruise.Checked = True Then
                strCruise = "1"
            Else
                strCruise = "0"
            End If
            If chkSafari.Checked = True Then
                strSafari = "1"
            Else
                strSafari = "0"
            End If
            If chkShortBreak.Checked = True Then
                strShortBreak = "1"
            Else
                strShortBreak = "0"
            End If
            If chkIsActive.Checked = True Then
                chkActive = "1"
            Else
                chkActive = "0"
            End If

            If cmbZones.SelectedValue = "" Then
                cmbZones.SelectedValue = 0
            End If

            If Request.QueryString("tid") Is Nothing Then
                Insert_Update_Delete_Record("INSERT INTO [ptn_Tours]([Name], [TourCode], [Days], [Nights], [Summary], [Description], [Include], [Exclude], [MetaTitle], [MetaKeywords], [MetaDescription], [IsFeatured], [IsPackage], [IsCruise], [IsSafari], [IsShortBreak], [IsActive], [Vieworder], [CreatedOn], [UpdatedOn]) VALUES('" & txtName.Text.Replace("'", "&#039;") & "','" & txtTourCode.Text.Replace("'", "&#039;") & "','" & txtDays.Text.Replace("'", "&#039;") & "','" & txtNights.Text.Replace("'", "&#039;") & "','" & txtSummary.Text.Replace("'", "&#039;") & "','" & txtDescription.Content.Replace("'", "&#039;") & "','" & txtInclude.Content.Replace("'", "&#039;") & "','" & txtExclude.Content.Replace("'", "&#039;") & "','" & txtMTitle.Text.Replace("'", "&#039;") & "','" & txtMKeywords.Text.Replace("'", "&#039;") & "','" & txtMDescription.Text.Replace("'", "&#039;") & "','" & strFeatured & "','" & strPackage & "','" & strCruise & "','" & strSafari & "','" & strShortBreak & "','" & chkActive & "','" & txtViewOrder.Text.Replace("'", "&#039;") & "','" & Date.Now & "','" & Date.Now & "')")

                Dim arr As Array
                arr = RetriveData("Select top 1 Id from ptn_Tours where Name='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "' and MetaTitle='" & Server.HtmlEncode(txtMTitle.Text.Replace("'", "&#039;")) & "' order by Id desc", 0)
                intId = arr(0)

                mapTour(cmbCategory.SelectedValue, cmbZones.SelectedValue, intId, False)

            Else
                Insert_Update_Delete_Record("Update [ptn_Tours] set [Name]='" & txtName.Text.Replace("'", "&#039;") & "', [TourCode]='" & txtTourCode.Text.Replace("'", "&#039;") & "', [Days]='" & txtDays.Text.Replace("'", "&#039;") & "', [Nights]='" & txtNights.Text.Replace("'", "&#039;") & "', [Summary]='" & txtSummary.Text.Replace("'", "&#039;") & "', [Description]='" & txtDescription.Content.Replace("'", "&#039;") & "', [Include]='" & txtInclude.Content.Replace("'", "&#039;") & "', [Exclude]='" & txtExclude.Content.Replace("'", "&#039;") & "', [MetaTitle]='" & txtMTitle.Text.Replace("'", "&#039;") & "', [MetaKeywords]='" & txtMKeywords.Text.Replace("'", "&#039;") & "', [MetaDescription]='" & txtMDescription.Text.Replace("'", "&#039;") & "', [IsFeatured]='" & strFeatured & "', [IsPackage]='" & strPackage & "', [IsCruise]='" & strCruise & "', [IsSafari]='" & strSafari & "', [IsShortBreak]='" & strShortBreak & "', [IsActive]='" & chkActive & "', [Vieworder]='" & txtViewOrder.Text.Replace("'", "&#039;") & "', [CreatedOn]='" & Date.Now & "', [UpdatedOn]='" & Date.Now & "' where Id=" & Request.QueryString("tid"))
                intId = Request.QueryString("tid")

                mapTour(cmbCategory.SelectedValue, cmbZones.SelectedValue, intId, True)

            End If
            UploadFile("ptn_Tours", "Thumbnail", fileUpload, intId, "/img")
            UploadFile("ptn_Tours", "GMap", fileMap, intId, "/maps")

            'uploadImage(intId, FileUpload, "thumbnail", "/img")
            'uploadImage(intId, fileMap, "gmap", "/maps")

            Response.Redirect("ViewTours.aspx")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Private Sub cmbCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCategory.SelectedIndexChanged

        Dim txt As Array
        txt = RetriveData("select distinct Id,Name from ptn_Zones z  inner join ptn_CatZoneTours cz on cz.ZoneId=z.Id where cz.CountryId='" & cmbCategory.SelectedValue & "' and IsActive=1 order by Name", 1)
        System.Threading.Thread.Sleep(2000)
        If Not txt(0) Is Nothing Then
            Fill_ComboBox(cmbZones, "select distinct Id,Name from ptn_Zones z  inner join ptn_CatZoneTours cz on cz.ZoneId=z.Id where cz.CountryId='" & cmbCategory.SelectedValue & "' and IsActive=1 order by Name")
            additem(cmbZones, "--Select--", 0)
            lblZone.Visible = False
            cmbZones.Enabled = True
        Else
            cmbZones.Items.Clear()
            additem(cmbZones, "--Select--", 0)
            lblZone.Visible = True
            lblZone.Text = "No zones available"
            lblZone.ForeColor = Color.Red
            cmbZones.Enabled = False
        End If

        'Fill_ComboBox(cmbZones, "select Id,Name from ptn_Zones z  inner join ptn_CatZoneTours cz on cz.ZoneId=z.Id where cz.CountryId=" & cmbCategory.SelectedValue & " and IsActive=1 order by Name")
        'additem(cmbZones, "--Select--", 0)
    End Sub

    Private Sub mapTour(ByVal CId As Integer, ByVal ZId As Integer, ByVal TId As Integer, ByVal IsUpdate As Boolean)
        Dim txt As Array
        If IsUpdate = True Then 'Update
            txt = RetriveData("Select CountryId, ZoneId from ptn_CatZoneTours where TourId=" & TId, 1)
            'Insert_Update_Delete_Record("Update ptn_CatZoneTours set CountryId=" & txt(0) & " and ZoneId=" & txt(1) & " and TourId=" & TId & " where CountryId=" & CId & " and ZoneId=" & ZId & " and TourId=" & TId)
            If Not txt(0) Is Nothing Then
                Insert_Update_Delete_Record("Update ptn_CatZoneTours set CountryId=" & CId & ", ZoneId=" & ZId & ", TourId=" & TId & " where CountryId=" & txt(0) & " and ZoneId=" & txt(1) & " and TourId=" & TId)
            Else
                Insert_Update_Delete_Record("Insert into ptn_CatZoneTours(CountryId, ZoneId, TourId) values(" & CId & "," & ZId & "," & TId & ")")
            End If
        Else
            If ZId <> 0 Then
                txt = RetriveData("Select max(TourId) from ptn_CatZoneTours where CountryId=" & CId & " and ZoneId=" & ZId, 0)

                If txt(0) = 0 Then
                    Insert_Update_Delete_Record("Update ptn_CatZoneTours set TourId=" & TId & " where CountryId=" & CId & " and ZoneId=" & ZId & " and tourId=0")
                ElseIf txt(0) <> 0 Then
                    Insert_Update_Delete_Record("Insert into dbo.ptn_CatZoneTours(CountryId, ZoneId, TourId) values(" & CId & "," & ZId & ", " & TId & ")")
                End If

            ElseIf ZId = 0 Then
                txt = RetriveData("Select max(TourId) from ptn_CatZoneTours where CountryId=" & CId, 0)

                If txt(0) = 0 Then
                    Insert_Update_Delete_Record("Update ptn_CatZoneTours set TourId=" & TId & " where CountryId=" & CId & " and ZoneId=" & ZId & " and tourId=0")
                ElseIf txt(0) <> 0 Then
                    Insert_Update_Delete_Record("Insert into dbo.ptn_CatZoneTours(CountryId, ZoneId, TourId) values(" & CId & "," & ZId & ", " & TId & ")")
                End If
            End If
        End If



        'Dim txt As Array
        'txt = RetriveData("Select max(TourId) from ptn_CatZoneTours where CountryId=" & CId & " and ZoneId=" & ZId & " and TourId=" & TId, 0)

        'If txt(0) = 0 And IsUpdate = False Then 'Insert
        '    Insert_Update_Delete_Record("Update ptn_CatZoneTours set TourId=" & TId & " where CountryId=" & CId & " and ZoneId=" & ZId & " and tourId=0")
        'ElseIf txt(0) <> 0 And IsUpdate = False Then 'Update
        '    Insert_Update_Delete_Record("Update ptn_CatZoneTours set TourId=" & TId & " where CountryId=" & CId & " and ZoneId=" & ZId & " and tourId=0")
        'End If

    End Sub

    'Private Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl1.SelectedIndexChanged
    '    MsgBox("Testing...")
    'End Sub

End Class